package com.neusoft.weekreport.component.controller;

import com.neusoft.weekreport.component.bizService.GroupBizService;
import com.neusoft.weekreport.component.req.GetAddGroupInfoReq;
import com.neusoft.weekreport.component.req.GetGroupInfoByIdReq;
import com.neusoft.weekreport.component.req.GetGroupInfoReq;
import com.neusoft.weekreport.component.resp.GetAddGroupInfoResp;
import com.neusoft.weekreport.component.resp.GetGroupInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetGroupInfoListResp;
import com.neusoft.weekreport.component.resp.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@EnableTransactionManagement
@RequestMapping("/group")
public class GroupController {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    GroupBizService groupBizService;

    //查询小组树信息
    @RequestMapping(value = "/groupManagement", method = RequestMethod.GET)
    public ResponseBean<GetGroupInfoListResp>  getGroupInfoList(GetGroupInfoReq req, HttpServletRequest request) {
        ResponseBean<GetGroupInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetGroupInfoListResp resp = groupBizService.getGroupInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"获取组信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //根据id查询小组信息
    @RequestMapping(value = "/groupById",method = RequestMethod.GET)
    public ResponseBean<GetGroupInfoByIdResp> getGroupInfoById(GetGroupInfoByIdReq req,HttpServletRequest request){
        ResponseBean<GetGroupInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetGroupInfoByIdResp resp =groupBizService.getGroupInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"获取组信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //根据id修改小组信息
    @RequestMapping(value = "/groupManagement",method = RequestMethod.PUT)
    public ResponseBean EditGroupInfoById(@RequestBody GetAddGroupInfoReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
            groupBizService.EditGroupInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"组信息修改成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //添加组
    @RequestMapping(value = "/groupManagement",method = RequestMethod.POST)
    public ResponseBean<GetAddGroupInfoResp> AddGroupInfoById(@RequestBody GetAddGroupInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddGroupInfoResp> responseBean = new ResponseBean<>();
        try{
            GetAddGroupInfoResp resp =groupBizService.AddGroupInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"组信息添加成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //删除组
    @RequestMapping(value = "/groupManagement",method = RequestMethod.DELETE)
    public ResponseBean DeleteGroupInfoById(@RequestBody GetGroupInfoByIdReq req,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try{
            groupBizService.DeleteGroupInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"删除组信息成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

}
